package com.zzb.myshowme.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zzb.myshowme.R;
import com.zzb.myshowme.bean.UserInfo;
import com.zzb.myshowme.presenter.PhoneLoginPresenter;
import com.zzb.myshowme.presenter.impl.PhoneLoginPresenterImpl;
import com.zzb.myshowme.utils.CustomToast;
import com.zzb.myshowme.view.IView.PhoneLoginView;
import com.zzb.myshowme.wiget.ProgressDialogView;

public class PhoneLoginActivity extends BaseActivity implements View.OnClickListener, PhoneLoginView {

    private ImageView back_imageV;
    private Button login_button;
    private TextView register_textV;
    private TextView password_textV;
    private EditText phone_editText;
    private EditText password_editText;

    private PhoneLoginPresenter presenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_login);

        initView();
        presenter = new PhoneLoginPresenterImpl(this);
    }

    private void initView() {
        back_imageV = (ImageView) findViewById(R.id.back_imageV);
        login_button = (Button) findViewById(R.id.login_button);
        register_textV = (TextView) findViewById(R.id.register_textV);
        password_textV = (TextView) findViewById(R.id.password_textV);
        phone_editText = (EditText) findViewById(R.id.phone_editText);
        password_editText = (EditText) findViewById(R.id.password_editText);

        back_imageV.setOnClickListener(this);
        login_button.setOnClickListener(this);
        register_textV.setOnClickListener(this);
        password_textV.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_imageV:
                finish();
                break;
            case R.id.register_textV:
                gotoSMSCode("register");
                break;
            case R.id.password_textV:
                gotoSMSCode("password");
                break;
            case R.id.login_button:
                //测试先关闭
//                presenter.login(phone_editText.getText().toString(), password_editText.getText().toString());
                UserInfo userinfo = new UserInfo();
                navigateToHome(userinfo);
                break;
        }
    }

    /**
     * 跳转到手机验证码界面
     */
    private void gotoSMSCode(String type) {
        Intent intent = new Intent(this, SendSecurityCodeActivity.class);
        intent.putExtra("type", type);
        startActivity(intent);
    }

    @Override
    public void showProgress() {
        ProgressDialogView.Initial(this, "登录中...");
        ProgressDialogView.show();
    }

    @Override
    public void hideProgress() {
        ProgressDialogView.dismiss();
    }

    /**--------------P 返回到 v 的 回调接口 --------------------***/
    @Override
    public void showToastError(String msg){
        CustomToast.show(this, msg, Toast.LENGTH_LONG);
    }

    @Override
    public void navigateToHome(UserInfo userinfo){
        Intent intent = new Intent(PhoneLoginActivity.this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);//开新堆栈，并清空之前的堆栈
        intent.putExtra("userinfo", userinfo);
        startActivity(intent);
        finish();
    }
}
